package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     * @param session
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R save(HttpSession session, @RequestBody ShoppingCart shoppingCart){
        //判断当前菜品或者套餐是否点过
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        if (shoppingCart.getSetmealId()!=null){//套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {//菜品
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        qw.eq(ShoppingCart::getUserId,id);
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one == null){ //不存在，添加数据
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(id);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else { //如果存在，数量+1
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
    }
    /**
     * 查询购物车
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R get(HttpSession session){
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,id);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }
    /**
     * 取消购物车
     * @param session
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R sub(HttpSession session,@RequestBody ShoppingCart shoppingCart){
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId()!=null){//菜品
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        qw.eq(ShoppingCart::getUserId,id);
        ShoppingCart one = shoppingCartService.getOne(qw);
        one.setNumber(one.getNumber()-1);
        if (one.getNumber() ==0){
            shoppingCartService.removeById(one.getId());
        }else {
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }
    /**
     * 清空购物车
     * @param session
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();
        shoppingCartService.removeById(id);
        return R.success("清空购物车成功");
    }
}
